package cn.wolfcode.shop.cloud.ws.websocket;

import lombok.Getter;
import lombok.Setter;
import org.springframework.stereotype.Component;

import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.util.concurrent.ConcurrentHashMap;

@Setter
@Getter

@ServerEndpoint("/{uuid}")//相当于http请求的reqstmapping

@Component

public class WebSocketServer {

    private Session session;

    public static ConcurrentHashMap<String,WebSocketServer> clients = new ConcurrentHashMap<>();

    @OnOpen

    public void onOpen(Session session, @PathParam( "uuid") String uuid){

        System.out.println("66666666666===>"+uuid);

        this.session = session;

        clients.put(uuid,this);

    }

    @OnClose

    public void onClose(@PathParam( "uuid") String uuid){

        clients.remove(uuid);

    }

    @OnError

    public void onError(Throwable error) {

        error.printStackTrace();

    }

}